package com.codingman.mybatisdemo1;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Demo1 {
	
	public static SqlSession getSqlSession() {
		InputStream is;
		try {
			is = Resources.getResourceAsStream("mybatis_config.xml");
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
			return factory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		SqlSession session = Demo1.getSqlSession();
		try {
			Employee e = new Employee(101, "Ann", 100.0);
                           // namespace.id
			session.insert("com.codingman.mybatisdemo1.EmployeeMapper.insertEmployee", e);
			session.commit();
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
	}

}
